﻿using AutoMapper;
using Nest;

namespace Journeo_backend.Services.Implementations
{
    public class TravelPlanService : ITravelPlanService
    {

        private readonly IMapper _mapper;
        private readonly ApplicationDbContext _context;
        public TravelPlanService(IMapper mapper, ApplicationDbContext context)
        {
            _mapper = mapper;
            _context = context;
        }
        // 创建旅行计划
        public async Task<Response<TravelPlan>> CreateTravelPlan(TravelPlanDTO travelPlanDTO)
        {
            TravelPlan travelPlan = _mapper.Map<TravelPlan>(travelPlanDTO);
            _context.TravelPlan.Add(travelPlan);

            try
            {
                await _context.SaveChangesAsync();
                return new Response<TravelPlan>(travelPlan);
            }
            catch (Exception e)
            {
                return new Response<TravelPlan>("error", e.Message);
            }

        }

        // 更新旅行计划
        public async Task<Response<TravelPlan>> UpdateTravelPlan(long planId, TravelPlanDTO travelPlanDTO)
        {
            //通过PlanId查找
            TravelPlan travelPlan = await _context.TravelPlan.FirstOrDefaultAsync(t => t.PlanId == planId);

            //不会为空
            if (travelPlan != null)
            {
                travelPlan = _mapper.Map(travelPlanDTO, travelPlan);
                travelPlan.UpdatedAt = DateTime.Now;
            }
            else
            {
                return new Response<TravelPlan>("error", Messages.FAILED);
            }

            try
            {
                await _context.SaveChangesAsync();
                return new Response<TravelPlan>(travelPlan);
            }
            catch (Exception e)
            {
                return new Response<TravelPlan>("error", e.Message);
            }
        }

        // 删除旅行计划
        public async Task<Response<string>> DeleteTravelPlan(long planId)
        {
            try
            {   //根据planId进行搜索
                TravelPlan travelPlan = await _context.TravelPlan.FirstOrDefaultAsync(t => t.PlanId == planId);
                if (travelPlan != null)
                {
                    _context.TravelPlan.Remove(travelPlan);
                }
                await _context.SaveChangesAsync();
                return new Response<string>(Messages.SUCCESS);
            }
            catch (Exception e)
            {
                return new Response<string>("error", e.Message);
            }
        }

        // 根据用户获取旅行计划
        public async Task<PageResponse<TravelPlan>> GetTravelPlans(long userId, int pageNumber, int pageSize)
        {
            try
            {
                // 根据userId进行搜索
                var query = _context.TravelPlan.Where(t => t.UserId == userId).OrderByDescending(t => t.CreatedAt);
                var totalCount = query.Count();
                var travelPlans = query.Skip((pageNumber - 1) * pageSize).Take(pageSize);
                return await Task.FromResult(new PageResponse<TravelPlan>(travelPlans.ToList(), pageNumber, pageSize, totalCount));
            }
            catch (Exception e)
            {
                return await Task.FromResult(new PageResponse<TravelPlan>("error", e.Message, null, pageNumber, pageSize, 0));
            }
        }

        // 根据计划Id获取旅行计划
        public async Task<Response<TravelPlan>> GetTravelPlan(long planId)
        {
            try
            {
                //主键查找
                TravelPlan travelPlan = await _context.TravelPlan.FindAsync(planId);
                return new Response<TravelPlan>(travelPlan);

            }
            catch (Exception e)
            {
                return new Response<TravelPlan>("error", e.Message);
            }
        }
    }
}
